
var info = [];

function addAddress(lat,lng, id,trackerId){
	var data = {lat:lat, lng:lng, reportId:id, addressFound:false,trackerId:trackerId};
	info.push(data);
}

function beginToAdd(){
	var data = null;

	while((data = info.pop()) != null){
		if(!data.addressFound){
			getOneAddress(data.lat, data.lng, data.reportId, data.trackerId);
			break;
		}
	}
	
}
function getOneAddress(lat, lng, reportId,trackerId){
	
    get_address(lat, lng,
            function(a, b, c) {
            	if(isNaN(a) || isNaN(b)){
            		beginToAdd();
            		return;
            	}
            	//console.log(lat,lng, a,b);
            	if(document.getElementById("address_"+reportId))
            	document.getElementById("address_"+reportId).innerHTML = c;
            	
            	Ext.Ajax.request({
            		method:'GET',
            		url: './gps_get_address.jsp',
            	    params: {action:'setreport',reportId:reportId, lat:a,lng:b, address:c, trackerId:trackerId},
            	    success:function(){
            			
            	    }
            	});
            	
            	
            	for (var i =0; i < info.length; i++) {
                    var data = info[i];//  GPS()
                    if (Math.abs(parseFloat(data.lat) - parseFloat(a)) < 0.0001 && Math.abs(parseFloat(data.lng) - parseFloat(b)) < 0.0001) {
                    	data.addressFound = true;
                    	if(document.getElementById("address_"+data.reportId))
                        document.getElementById("address_"+data.reportId).innerHTML = c;
                    	
                    	Ext.Ajax.request({
                    		method:'GET',
                    		url: './gps_get_address.jsp',
                    	    params: {action:'setreport',reportId:data.reportId, lat:a,lng:b, address:c, trackerId:trackerId},
                    	    success:function(){
                    			
                    	    }
                    	});
                    }
                }
            	beginToAdd();
            });
}